/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：Article_goods接口实现                                                    
*│　作    者：NetCoreCMS                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-12-25 10:52:46                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： NetCoreCMS.Repository.SqlServer                                  
*│　类    名： Article_goodsRepository                                      
*└──────────────────────────────────────────────────────────────┘
*/
using NetCoreCMS.DbHelper;
using NetCoreCMS.IRepository;
using NetCoreCMS.Models;
using Dapper;
using Microsoft.Extensions.Options;
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Transactions;

namespace NetCoreCMS.Repository.SqlServer
{
    public class Article_goodsRepository:BaseRepository<Article_goods,Int32>, IArticle_goodsRepository
    {
        public Article_goodsRepository(IOptionsSnapshot<DbOption> options)
        {
            _dbOption =options.Get("NetCoreCMS");
            if (_dbOption == null)
            {
                throw new ArgumentNullException(nameof(DbOption));
            }
            //_dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString);
        }

        public void Add(Article_goods model, int article_id)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
                {
                    #region 主表信息==========================
                    model.id = 0;
                    int id = (int)_dbConnection.Insert(model);
                    #endregion

                    #region 自定义会员组价格==================
                    if (model.group_prices != null)
                    {
                        foreach (Models.User_group_price modelt in model.group_prices)
                        {
                            User_group_price price = new User_group_price();
                            price.article_id = article_id;
                            price.goods_id = id;
                            price.group_id = modelt.group_id;
                            price.price = modelt.price;
                            price.pv_price = modelt.pv_price;

                            {

                            }
                            _dbConnection.Insert<User_group_price>(price);
                        }
                    }
                    #endregion
                    scope.Complete();
                }
        }
        }

        public bool DeleteAll(int id)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                return _dbConnection.Execute("delete from [" + nameof(User_group_price) + "] where article_id=@article_id;delete from [Article_goods] where article_id=@article_id", new { article_id = id }) > 0;
            }
        }

        public List<Article_goods> GetList(int article_id)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                var list = _dbConnection.GetList<Article_goods>("where article_id=@article_id", new { article_id = article_id }).ToList();
                for (int i = 0; i < list.Count; i++)
                {
                    list[i].group_prices = _dbConnection.GetList<Models.User_group_price>("where goods_id=@id", new { id = list[i].id }).ToList();
                }
                return list;
            }
        }
    }
}